/**
 * 
 */
package com.gr.staffpm.charts;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.joda.time.DateTime;
import org.wicketstuff.flot.Color;
import org.wicketstuff.flot.DataSet;
import org.wicketstuff.flot.LineGraphType;

import com.gr.staffpm.datatypes.Feedback;
import com.gr.staffpm.datatypes.Task;


/**
 * @author Graham Rhodes 6 Apr 2011 09:31:54
 */
public class PredefinedCharts {

    public static List<Series> createdVsResolvedChart(List<Task> tasksCreated, List<Task> tasksResolved, int days) {
        List<Series> result = new ArrayList<Series>();

        DateTime date = new DateTime().withTime(0, 0, 0, 0).minusDays(days);

        List<DataSet> tasksCreatedDataSet = new ArrayList<DataSet>();
        List<DataSet> tasksResolvedDataSet = new ArrayList<DataSet>();
        for (int i = 0; i <= days; i++) {
            int noResolvedTasks = 0, noCreatedTasks = 0;
            for (Task task : tasksCreated) {
                DateTime created = new DateTime(task.getCreated());
                if (created.withTime(0, 0, 0, 0).equals(date.withTime(0, 0, 0, 0)))
                    noCreatedTasks++;
            }
            for (Task task : tasksResolved) {
                DateTime resolved = new DateTime(task.getResolvedDate());
                if (resolved.withTime(0, 0, 0, 0).equals(date.withTime(0, 0, 0, 0)))
                    noResolvedTasks++;
            }

            tasksCreatedDataSet.add(new DataSet(date, noCreatedTasks));
            tasksResolvedDataSet.add(new DataSet(date, noResolvedTasks));
            date = date.plusDays(1);
        }

        result.add(new Series(tasksCreatedDataSet, "Tasks Created", new Color(0xE6, 0x16, 0x16), new LineGraphType(null, false, null)));
        result.add(new Series(tasksResolvedDataSet, "Tasks Resolved", new Color(0x19, 0xD1, 0x19), new LineGraphType(null, false, null)));

        return result;
    }

    public static List<Series> recentFeedback(List<List<Feedback>> stars, List<Color> colors, int days) {
        List<Series> result = new ArrayList<Series>();
        DateTime date = new DateTime().withTime(0, 0, 0, 0).minusDays(days);
        @SuppressWarnings("unchecked")
        List<ArrayList<DataSet>> dataSetList =
                Arrays.asList(new ArrayList<DataSet>(), new ArrayList<DataSet>(), new ArrayList<DataSet>(), new ArrayList<DataSet>(), new ArrayList<DataSet>(), new ArrayList<DataSet>());

        for (int i = 0; i < stars.size(); i++) {
            List<Feedback> dayFeedbackList = stars.get(i);
            List<Integer> noRecStars = Arrays.asList(0, 0, 0, 0, 0, 0);
            for (Feedback feed : dayFeedbackList) {
                DateTime created = new DateTime(feed.getCreated());
                if (created.withTime(0, 0, 0, 0).equals(date.withTime(0, 0, 0, 0)))
                    noRecStars.set(feed.getRating(), noRecStars.get(feed.getRating()) + 1);
            }
            dataSetList.get(0).add(new DataSet(date, noRecStars.get(0)));
            dataSetList.get(1).add(new DataSet(date, noRecStars.get(1)));
            dataSetList.get(2).add(new DataSet(date, noRecStars.get(2)));
            dataSetList.get(3).add(new DataSet(date, noRecStars.get(3)));
            dataSetList.get(4).add(new DataSet(date, noRecStars.get(4)));
            dataSetList.get(5).add(new DataSet(date, noRecStars.get(5)));

            date = date.plusDays(1);
        }

        for (int i = 0; i < dataSetList.size(); i++) {
            ArrayList<DataSet> dataSet = dataSetList.get(i);
            result.add(new Series(dataSet, i + " Stars received", colors.get(i), new LineGraphType(null, false, null)));
        }
        return result;
    }

    public static List<Series> feedback(DateTime start, List<List<Feedback>> stars, List<Color> colors, int days) {
        List<Series> result = new ArrayList<Series>();
        DateTime date = start.withTime(0, 0, 0, 0).minusDays(days);
        @SuppressWarnings("unchecked")
        List<ArrayList<DataSet>> dataSetList =
                Arrays.asList(new ArrayList<DataSet>(), new ArrayList<DataSet>(), new ArrayList<DataSet>(), new ArrayList<DataSet>(), new ArrayList<DataSet>(), new ArrayList<DataSet>());

        for (int i = 0; i < stars.size(); i++) {
            List<Feedback> dayFeedbackList = stars.get(i);
            List<Integer> noRecStars = Arrays.asList(0, 0, 0, 0, 0, 0);
            for (Feedback feed : dayFeedbackList) {
                DateTime created = new DateTime(feed.getCreated());
                if (created.withTime(0, 0, 0, 0).equals(date.withTime(0, 0, 0, 0)))
                    noRecStars.set(feed.getRating(), noRecStars.get(feed.getRating()) + 1);
            }
            dataSetList.get(0).add(new DataSet(date, noRecStars.get(0)));
            dataSetList.get(1).add(new DataSet(date, noRecStars.get(1)));
            dataSetList.get(2).add(new DataSet(date, noRecStars.get(2)));
            dataSetList.get(3).add(new DataSet(date, noRecStars.get(3)));
            dataSetList.get(4).add(new DataSet(date, noRecStars.get(4)));
            dataSetList.get(5).add(new DataSet(date, noRecStars.get(5)));

            date = date.plusDays(1);
        }

        for (int i = 0; i < dataSetList.size(); i++) {
            ArrayList<DataSet> dataSet = dataSetList.get(i);
            result.add(new Series(dataSet, i + " Stars received", colors.get(i), new LineGraphType(null, false, null)));
        }
        return result;
    }

}
